/*
 * Wantmore - Service Delivery Platform.
 *
 * $Id: ImageEditFromController.java 60 2010-10-13 07:26:24Z alexey.batyuta@gmail.com $
 *
 * Copyright 2010 by Sunbay Software Crimea Ltd.
 * All rights reserved
 */
package ua.crimea.bat.ss.forms;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.MultipartFile;
import ua.crimea.bat.ss.model.beans.Image;
import ua.crimea.bat.ss.model.validators.ImageValidator;
import ua.crimea.bat.ss.services.PhotoAlbumsService;

import java.io.IOException;

/**
 * User: bat
 * Date: Mar 26, 2010
 * Time: 9:28:09 AM
 */
@Controller("imageEditFromController")
@RequestMapping("/albums/*/images/{imageId}/edit")
@SessionAttributes("image")
public class ImageEditFromController {
    protected PhotoAlbumsService photoAlbumsService;

    public ImageEditFromController() {
    }

    public void setPhotoAlbumsService(PhotoAlbumsService photoAlbumsService) {
        this.photoAlbumsService = photoAlbumsService;
    }

    public PhotoAlbumsService getPhotoAlbumsService() {
        return photoAlbumsService;
    }

    @InitBinder
    public void setAllowedFields(WebDataBinder dataBinder) {
        dataBinder.setDisallowedFields("id");
    }

    @Autowired
    public ImageEditFromController(PhotoAlbumsService photoAlbumsService) {
        setPhotoAlbumsService(photoAlbumsService);
    }

    @RequestMapping(method = RequestMethod.GET)
    public String setupForm(@PathVariable("imageId") int imageId, Model model) {
        Image image = photoAlbumsService.loadImage(imageId);
        model.addAttribute("image", image);
        return "images/form";
    }

    @RequestMapping(method = {RequestMethod.PUT, RequestMethod.POST})
    public String processSubmit(@ModelAttribute("image") Image image, @RequestParam("image1") MultipartFile imageBlob, @RequestParam("thumb") MultipartFile thumbBlob, BindingResult result, SessionStatus status) {
        new ImageValidator().validate(image, result);
        if (result.hasErrors()) {
            return "images/form";
        } else {
            if (!imageBlob.isEmpty()) {
                try {
                   byte[] bytes = imageBlob.getBytes();
                    image.setImage(bytes);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (!thumbBlob.isEmpty()) {
                try {
                    byte[] bytes = thumbBlob.getBytes();
                    image.setThumb(bytes);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            photoAlbumsService.storeImage(image);
            status.setComplete();
            return "redirect:/albums/" + image.getAlbum().getId();
        }
    }

    @RequestMapping(method = RequestMethod.DELETE)
    public String deleteImage(@PathVariable int imageId) {
        Image image = photoAlbumsService.loadImage(imageId);
        photoAlbumsService.deleteImage(imageId);
        return "redirect:/albums/" + image.getAlbum().getId();
    }
}
